<?php defined('SYSPATH') or die('No direct script access.');

class Calendar_Events_Controller extends Template_Controller 
{
	public function __construct()
	{
		parent::__construct();

		$this->template->menu = new View('menu');
	}
	
	public function index()
	{		
		$model = new Calendar_Event_Model();
		
		$table = new View('calendar/events_lists');
		$table->caption = 'Calendar Events List';
		$table->datas = $model->find_all();;
		
		$this->template->content = $table->render();
	}
	
	public function create()
	{
		$view = new View('calendar/events_form');
		$model = new Calendar_Category_Model();
		$event = new Calendar_Event_Model();
		
		$view->categories = $model->find_all();
		$view->event = $event;
		
		if(isset($_POST['submit']))
		{
			$event->name = $_POST['name'];
			$event->title_cn = $_POST['title_cn'];
			$event->title_en = $_POST['title_en'];
			$event->content_cn = $_POST['content_cn'];
			$event->content_en = $_POST['content_en'];
			$event->orgnization_cn = $_POST['orgnization_cn'];
			$event->orgnization_en = $_POST['orgnization_en'];
			$event->location = $_POST['location'];
			$event->website = $_POST['website'];
			$event->telephone = $_POST['telephone'];
			$event->start_date = $_POST['start_date'];
			$event->end_date = $_POST['end_date'] <= $_POST['start_date'] ? $_POST['start_date'] : $_POST['end_date'];
			$event->start_time = $_POST['start_time_h'].':'.$_POST['start_time_s'].':00';
			$event->end_time = $_POST['end_time_h'].':'.$_POST['end_time_s'].':00';
			$event->category_id = $_POST['category_id'];
			$event->created_at = date('Y-m-d H:m:i');
 
			//Save form into table
			if($event->save())
			{
				url::redirect('calendar_events');
			}
		}
		
		$this->template->content = $view->render();
	}
	
	public function edit($id)
	{
		$view = new View('calendar/events_form');
		$model = new Calendar_Category_Model();
		$event = new Calendar_Event_Model($id);
		
		$view->categories = $model->find_all();
		$view->event = $event;
		
		if(isset($_POST['submit']))
		{
			//Load model and set values
			$event->name = $_POST['name'];
			$event->title_cn = $_POST['title_cn'];
			$event->title_en = $_POST['title_en'];
			$event->content_cn = $_POST['content_cn'];
			$event->content_en = $_POST['content_en'];
			$event->orgnization_cn = $_POST['orgnization_cn'];
			$event->orgnization_en = $_POST['orgnization_en'];
			$event->location = $_POST['location'];
			$event->website = $_POST['website'];
			$event->telephone = $_POST['telephone'];
			$event->start_date = $_POST['start_date'];
			$event->end_date = $_POST['end_date'] <= $_POST['start_date'] ? $_POST['start_date'] : $_POST['end_date'];
			$event->start_time = $_POST['start_time_h'].':'.$_POST['start_time_s'].':00';
			$event->end_time = $_POST['end_time_h'].':'.$_POST['end_time_s'].':00';
			$event->category_id = $_POST['category_id'];
 
			//Save form into table
			if($event->save())
			{
				url::redirect('calendar_events');
			}
		}
		
		$this->template->content = $view->render();
	}
	
	public function delete($id)
	{
		$result = new Calendar_Category_Model($id);
		$result->delete();
		
		url::redirect('calendar_category');
	}

}